package com.ruoyi.cyinfo.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.excel.EasyExcel;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cyinfo.domain.InfoIdiom;
import com.ruoyi.cyinfo.service.IInfoIdiomService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 成语库Controller
 *
 * @author ruoyi
 * @date 2024-03-10
 */
@RestController
@RequestMapping("/infoIdiom/idiom")
public class InfoIdiomController extends BaseController {
    @Autowired
    private IInfoIdiomService infoIdiomService;

    /**
     * 查询成语库列表
     */
    @GetMapping("/list")
    public TableDataInfo list(InfoIdiom infoIdiom) {
        startPage();
        List<InfoIdiom> list = infoIdiomService.selectInfoIdiomList(infoIdiom);
        return getDataTable(list);
    }

    /**
     * 导出成语库列表
     */
    @Log(title = "成语库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoIdiom infoIdiom) {
        List<InfoIdiom> list = infoIdiomService.selectInfoIdiomList(infoIdiom);
        ExcelUtil<InfoIdiom> util = new ExcelUtil<InfoIdiom>(InfoIdiom.class);
        util.exportExcel(response, list, "成语库数据");
    }

    /**
     * 获取成语库详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(infoIdiomService.selectInfoIdiomById(id));
    }

    /**
     * 新增成语库
     */
    @Log(title = "成语库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InfoIdiom infoIdiom) {
        return toAjax(infoIdiomService.insertInfoIdiom(infoIdiom));
    }

    /**
     * 修改成语库
     */
    @Log(title = "成语库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoIdiom infoIdiom) {
        return toAjax(infoIdiomService.updateInfoIdiom(infoIdiom));
    }

    /**
     * 删除成语库
     */
    @Log(title = "成语库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(infoIdiomService.deleteInfoIdiomByIds(ids));
    }


    /*
    * 导入成语
    * */
    @PostMapping("/importIdiom")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importExcel(MultipartFile file) {

        List<InfoIdiom> infoIdioms;
        try {
            //使用EasyExcel读取数据
            //设置输入流
            infoIdioms = EasyExcel.read(file.getInputStream())
                    //设置读取对象
                    .head(InfoIdiom.class)
                    //设置读取sheet默认读取第一个
                    .sheet()
                    //设置所在标题
                    .headRowNumber(1)
                    //设置异步读取
                    .doReadSync();

        } catch (IOException e) {
            throw new RuntimeException("读取失败！");
        }
        for (InfoIdiom infoIdiom : infoIdioms) {
            if (StringUtils.isNull(infoIdiom)){
                throw new RuntimeException("请检查数据格式");
            }
            infoIdiomService.insertInfoIdiom(infoIdiom);
        }
        return AjaxResult.success();
    }

}
